package com.utilities.parsers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import com.utilities.IO.FileUtilities;
import com.utilities.dataStructures.Range;

public class PrimerGenerator {

	String gapList = null;
	String segmentList = null;
	public PrimerGenerator(String gapList, String segmentList)
	{
		this.gapList = gapList;
		this.segmentList = segmentList;
	}

	public ArrayList<Range> findPrimers() throws Exception
	{
		ArrayList<Range> ranges = FileUtilities.readFromFile(gapList);
		ArrayList<Range> outRange = new ArrayList<Range>();
		int lastIndex = 0;
		for(Range range : ranges)
		{
			int startIndex = range.getLowerLimit();
			int endIndex = range.getUpperLimit();
			int size = endIndex - startIndex + 1;
			if (size < 1600)
			{
				int midpoint = startIndex + (size/2);
				int _start = midpoint - 800;
				int _end = _start + 1599;
				Range r = new Range(_start, _end, range.getMetadata());
				for (Range segment : outRange)
				{
					if (segment.contains(range))
					{
						segment.setMetadata(segment.getMetadata() + ":" + range.getMetadata());
					}
				}
				outRange.add(r);
				lastIndex = _end;
			}
		}
		Collections.sort(outRange,new Comparator<Range>(){

			@Override
			public int compare(Range o1, Range o2) {
				int one = o1.getMetadata().split(":").length;
				int two = o2.getMetadata().split(":").length;
				
				if (one != two)
				{
					return two - one;
				}
				else
				{
					return o1.getLowerLimit() - o2.getLowerLimit();
				}
			}
		});
		ArrayList<Range> last = new ArrayList<Range>();
		for (Range r : outRange)
		{
			boolean flag = true;
			for(Range r1 : last)
			{
				if (r1.exclusive(r) || 
						((r.getUpperLimit() - r.getLowerLimit()) <= 20 || (r1.getUpperLimit() - r.getLowerLimit()) <= 20))
				{
					flag = true;
				}
				else
				{
					flag = false;
					break;
				}
			}
			if (flag)
			{
				last.add(r);
			}
		}
		Collections.sort(last, new Comparator<Range>(){

			@Override
			public int compare(Range o1, Range o2) {
				int diff =  o1.getLowerLimit() - o2.getLowerLimit();
				if (diff == 0)
				{
					diff = o1.getUpperLimit() - o2.getUpperLimit();
				}
				return diff;
			}	
		
		});
		
//		ArrayList<Range> rangeList = new ArrayList<Range>();
//		for(int i = 0 ; i < outRange.size() ; )
//		{
//			Range considered = outRange.get(i);
//			int numberOfGapsCovered = considered.getMetadata().split(":").length;
//			while(i < outRange.size())
//			{
//				i++;
//				Range overLap = outRange.get(i);
//				int overLapSize = considered.getUpperLimit() - overLap.getLowerLimit();
//				if (overLapSize > 20)
//				{
//					int overLapGapsCovered = overLap.getMetadata().split(":").length;
//					if (overLapGapsCovered > numberOfGapsCovered)
//					{
//						numberOfGapsCovered = overLapGapsCovered;
//						considered = overLap;
//					}
//				}
//				else
//				{
//					break;
//				}
//			}
//			rangeList.add(considered);
//		}
		FileUtilities.WriteToFile(last, segmentList);
		return last;
	}

	public static void main(String[] args) throws Exception
	{
		PrimerGenerator generator = new PrimerGenerator("C:/Users/bansalp/Desktop/UZH/newCosmid/run1run2/all/gapList.txt", 
		"C:/Users/bansalp/Desktop/UZH/newCosmid/run1run2/all/segmentList.txt");
		generator.findPrimers();
	}

}
